package com.zkthink.ceres.applet.propertyDetail.controller;

import com.zkthink.base.R;
import com.zkthink.ceres.applet.authority.domain.entity.AppletMember;
import com.zkthink.ceres.applet.authority.service.AppletMemberService;
import com.zkthink.ceres.applet.comment.service.AppletCommentService;
import com.zkthink.ceres.applet.commission.domain.entry.AppletCommission;
import com.zkthink.ceres.applet.commission.domain.vo.AppletCommissionVO;
import com.zkthink.ceres.applet.commission.mapper.AppletCommissionMapper;
import com.zkthink.ceres.applet.commission.service.AppletCommissionService;
import com.zkthink.ceres.applet.order.mapper.AppletProductOrderMapper;
import com.zkthink.ceres.applet.order.mapper.AppletServerOrderMapper;
import com.zkthink.ceres.applet.order.service.AppletProductOrderService;
import com.zkthink.clh.order.dto.MyqianbaoDto;
import com.zkthink.clh.order.vo.MyqianbaoVo;
import com.zkthink.clh.order.vo.MyqianbaodetailsVo;
import com.zkthink.zhl.order.service.ChlOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static com.zkthink.ceres.applet.authority.config.AuthorityWebConfiguration.getMid;

/**
 * @Author cmz
 * @Date 2024-04-28 17:41
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/applet/propertyDetail")
@Api(value = "propertyDetail", tags = "我的钱包")
public class AppletPropertyDetailController {
    @Autowired
    private AppletCommentService appletCommentService;
    
    @Autowired
    private AppletMemberService appletMemberService;

    @Autowired
    private ChlOrderService chlOrderService;
    
    @Autowired
    private AppletServerOrderMapper appletServerOrderMapper;
    
    @Autowired
    private AppletProductOrderMapper appletProductOrderMapper;
    @Autowired
    private AppletProductOrderService appletProductOrderService;
    @Autowired
    private AppletCommissionMapper commissionMapper;
    @Autowired
    private AppletCommissionService appletCommissionService;

    @ApiOperation(value = "获取当前登录用户余额", notes = "获取当前登录用户余额")
    @GetMapping(value = "/getBalance")
    public R getBalance() {
        return appletMemberService.getBalance();
    }
    
    @ApiOperation("我的钱包详情")
    @PostMapping("/myqianbao")
    public R myqianbao(@RequestBody MyqianbaoDto dto) {
//        if (dto.getMid()==null){
            dto.setMid(getMid());
//        }
        MyqianbaoVo vo = (MyqianbaoVo) chlOrderService.myqianbao(dto).getData();
        vo = appletProductOrderService.myQianBao(dto,vo);
        List<MyqianbaodetailsVo> list = vo.getList();
        if (list.size() >0) {
            list = list.stream().sorted(Comparator.comparing(MyqianbaodetailsVo::getPayTime).reversed()).collect(Collectors.toList());
            vo.setList(list);
        }
        //累计收入
        return R.success(vo);
    }

    @ApiOperation("我的推荐明细")
    @GetMapping("/myRecommend")
    public R myRecommend() {
        List<AppletCommissionVO> appletCommissions = appletCommissionService.myRecommend();
        if (appletCommissions.size() == 0) {
            return R.fail("暂无数据");
        }
        //累计收入
        return R.success(appletCommissions);
    }
}
